package com.mhkj.revise.mainPlan.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.busiMode.service.BusiModelService;
import com.mhkj.base.busiType.service.BusinessTypeService;
import com.mhkj.base.itemInfo.item.service.ItemService;
import com.mhkj.revise.mainPlan.service.MainPlanNodeService;
import com.mhkj.revise.plan.entity.PlanNode;
import com.mhkj.revise.plan.service.PlanNodeService;
import com.mics.core.common.componet.JsonHandler;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.organizational.bean.Office;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 主项计划
 * @author mics
 * @date 2018年8月18日
 * @version 1.0
 */
@Controller
@RequestMapping("/mainPlanNode")
public class MainPlanNodeController extends BaseController<PlanNode>{
	@Resource
	private MainPlanNodeService mainPlanNodeService;
	@Resource
	private BusinessTypeService businessTypeService;
	@Resource
	private ItemService itemService;
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	private OrgService orgService;
	@Resource
	private BusiModelService busiModelService;
	@Resource
	private JsonHandler jsonHandler;

	@Resource
	public void setPlanNodeService(PlanNodeService planNodeService) {
		super.setBaseService(planNodeService);
	}

	@InitBinder
	protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}

	@RequestMapping("/listByPage")
	@ResponseBody
	public void listByPage(HttpServletResponse response, PlanNode planNode, int pageIndex, int pageSize) {
		Pagination<PlanNode> pagination = mainPlanNodeService.listByPage(planNode, pageIndex, pageSize);
		jsonHandler.sendJsonString(response, true, "获取数据成功", pagination,new String[]{
				"id","NO","business.typeName","nodeName","nodeProperty.value","completeStandard","deadline","deadlineDesc","beginDate","endDate","remark","companyName","officeName","postName"},
				"id","NO","business","nodeName","nodeProperty","completeStandard","deadline","deadlineDesc","beginDate","endDate","remark","companyName","officeName","postName");
	}


	
	@Override
	@RequestMapping("/save")
	@ResponseBody
	public ResponseMessage save(PlanNode t) {
		String message = mainPlanNodeService.save(t);
		if (message == null) {
			return ResponseMessage.createMessage(true, "保存计划节点成功！");
		} else {
			return ResponseMessage.createMessage(false, message);
		}
	}


	@Override
	@RequestMapping("/info")
	@ResponseBody
	public PlanNode info(long id) {
		PlanNode planNode = super.info(id);
		Office cOffice = orgService.officeInfo(planNode.getOfficeId());
		planNode.setOfficeName(cOffice.getName());
		return planNode;
	}


}
